﻿using System.Collections;
using System.Collections.Generic;
using System;
using UnityEngine;
using Assets.Scripts.TDu3DWork;
using Assets.Scripts.TDuResource;

namespace Assets.Scripts.TDu3DWork
{
    class TDuSceneCfgWorker : TDuSceneWorker
    {
        public TDuPostActionWorker m_postActoinWorker;
        public TDuVRDevicesWorker m_controllerWorker;
        public TDuCmdActionWorker m_cmdActionWorker;

        public TDuSceneCfgWorker(string strCfgUrl) : base( strCfgUrl )
        {
            m_postActoinWorker = new TDuPostActionWorker();
            TDuPostEventArgs.registerPostActionDictionary();

            m_controllerWorker = new TDuVRDevicesWorker();

            m_cmdActionWorker = new TDuCmdActionWorker();
        }

        public TDuSceneCfgWorker()
        {

        }

        //初始化视点;
        public void initViewport()
        {
            if( m_sceneCfg.vecViewPorts.Count > 0)
            {
                setViewPort(0);
            }
        }
        public void initVrViewport()
        {
            setVrViewPort();
        }
        //执行第一步;
        public void workInFirstStep()
        {
            string strVarName = "第一步";
            //string strVarName = "语音播报";

            VariableNode varNode = null;
            if( !m_sceneCfg.vecVariableNode.ContainsKey(strVarName))
            {
                m_sceneCfg.vecVariableNode.TryGetValue("开始", out varNode);                
            }
            else
            {
                m_sceneCfg.vecVariableNode.TryGetValue(strVarName, out varNode);
            }

            //显示提示文本
            m_uiManager.updateMainNavigatorTipText(varNode.strVarText);

            //处理开始步骤的后置动作;
            m_strCurStepName = strVarName;
            updateMsgVar(strVarName, true);

            //搜寻下一步;
            searchNextStep("", "");
        }

        //初始化工作器;
        public void initWorker()
        {
            //为后置动作器添加委托;
            postActionHandler += m_postActoinWorker.onPostAction;

            //为手柄控制器添加委托;
            controllerHandler += m_controllerWorker.onControllerAction;

            //为shell控制器添加委托;
            shellHandler += m_cmdActionWorker.onCmdAction;

            if( !m_bIsVR )
                initViewport();
            else//VR 模式根据场景来配置初始视点 TDuProfiler
            {
                initVrViewport();
            }
            //添加UI;
            m_uiManager.createMainNavigator(this, m_mainCanvas);


            //增加脚本
            #region
            //试用开始步骤测试其他UI;
            string strVarName = "第一步";
            //string strVarName = "语音播报";

            VariableNode varNode = null;
            if (!m_sceneCfg.vecVariableNode.ContainsKey(strVarName))
            {
                m_sceneCfg.vecVariableNode.TryGetValue("开始", out varNode);
               
            }
            else
            {
                m_sceneCfg.vecVariableNode.TryGetValue(strVarName, out varNode);
                Debug.Log(m_sceneCfg.mapSteps);
            }

            //m_uiManager.createDialogDashboard(this, varNode, m_popupCanvas);
            //m_uiManager.createTipPictureDashboard(this,varNode, m_popupCanvas);
            //m_uiManager.createVideoWndDashboard(this, varNode, m_popupCanvas);
            #endregion
        }

        public IEnumerator parsingCfg()
        {
            yield return new WaitWhile(parsingSceneCfg);
        }

        //场景工作
        public void start()
        {
            //同步解析配置文件;
            parsingSceneCfg();

            //初始化工作器;
            initWorker();

            //执行第一步;
            workInFirstStep();
        }

    }
}